package com.ltu.timkor6.gamesite.users.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.ltu.timkor6.gamesite.shared.UserInfo;

public class Profile
{
	public static final String REGEX_EMAIL = "^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
	
	private ProfileComposite composite;
	private Users users;
	
	private UsersServiceAsync service = GWT.create(UsersService.class);
		
	public Profile(Users users)
	{
		this.users = users;
		checkUser();
		composite = new ProfileComposite(this);
	}
	
	public UserInfo getUserInfo()
	{
		return users.getUserInfo();
	}
	
	public void checkSubmit()
	{
		boolean error = false;
		
		UserInfo profileUser = new UserInfo();
		
		String password = composite.getPassword();
		String rePassword = composite.getRePassword();
		String email = composite.getEmail();
		boolean testUser = composite.getTestUser();
		
		composite.clearErrors();

		if(password.compareTo(rePassword) != 0)
		{
			error = true;
			
			composite.addError("Passwords does not match.");
		}
		
		if(email.isEmpty())
		{
			error = true;
			
			composite.addError("Email cannot be empty.");
		}
		else if(!email.matches(REGEX_EMAIL))
		{
			error = true;
			
			composite.addError("Email is not valid.");
		}
		
		if(!error)
		{
			profileUser.setPassword(password);
			profileUser.setEmail(email);
			profileUser.setIsTestUser(testUser);
			
			sendProfile(profileUser);
		}
	}
	
	/**
	 * For user authentification
	 */
	public void checkUser()
	{
		// Initialize the service proxy.
		if(service == null)
		{
			service = GWT.create(UsersService.class);
		}
		
		// Set up the callback object.
		AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>()
		{
			public void onFailure(Throwable caught)
			{
				Window.alert("Error: " + caught.getMessage());
			}
			
			public void onSuccess(Boolean signedIn)
			{
			}
		};
		
		// Make the call to the stock price service.
		service.checkUser(callback);
	}
	
	private void sendProfile(UserInfo profileUser)
	{
		// Initialize the service proxy.
		if(service == null)
		{
			service = GWT.create(UsersService.class);
		}
		
		// Set up the callback object.
		AsyncCallback<SignInStatus> callback = new AsyncCallback<SignInStatus>()
		{
			public void onFailure(Throwable caught)
			{
				Window.alert("Error: " + caught.getMessage());
			}
			
			public void onSuccess(SignInStatus status)
			{
				if(status == SignInStatus.SUCCESS)
				{
					users.updatedUserInfo();
				}
				else if(status == SignInStatus.NO_USER)
				{
					composite.addError("Username does not exists.");
				}
				else
				{
					composite.addError("Something went wrong with the server (" + SignInStatus.UNKNOWN + ").");
				}
			}
		};
		
		// Make the call to the service.
		service.profile(profileUser, callback);
	}
	
	public Composite getComposite()
	{
		return composite;
	}

	public void drawAgain()
	{
		composite.drawAgain();
	}
}
